﻿@{
    ViewBag.Title = "新试验";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@using TrialManagementTool.Helper

<style type="text/css">
    .ui-th-column {
        background-color: #ffffff !important;
        color: #333333 !important;
        font-weight: bold !important;
        font-size: 14px;
    }

    .mainCont {
        top: 60px;
    }

    .provinceSelect {
        width: auto;
    }

        .provinceSelect ul {
            width: auto;
        }
</style>
<script src="@TrialManagementTool.Helper.UrlHelper.alterContent("Scripts/js/trial.js")"></script>

<div class="mainCont">
    <div class="trialFirstPage">
        <label class="lableword"><span class="splitline">|</span>基本信息</label>
        <div>
            <label class="selectitem">TTM</label> <span class="currentUser">@UserHelper.GetCurrentAdminUser().Name</span>
            <label class="selectitem">项目类别<span class="markstar">&nbsp;*</span></label>
            <select id="projectCategory"></select>
            <label class="selectitem">CA</label>
            <input type="text" id="CAName" value="" placeholder="请输入" />
            <label class="selectitem">FSM</label>
            <input type="text" id="FSMName" value="" placeholder="请输入" />
        </div>
        <label class="lableword"><span class="splitline">|</span>示例方案</label>
        <div class="newtrial">
            <label class="trialword">产品类别<span class="markstar">&nbsp;*</span></label>
            <select id="productCategory" onchange="GetProductByCategory()"></select>
            <label class="selectitem">产品<span class="markstar">&nbsp;*</span></label>
            <select id="productItem" onchange="ProductSelect()"><option>请先选择产品分类</option></select>
            <label class="selectitem" id="othersproductlabel" style="display:none">其它<span class="markstar">&nbsp;*</span></label>
            <input type="text" id="ProductName" value="" placeholder="请输入" style="display:none" maxlength="50" />
        </div>
        <div class="newtrial">
            <label class="trialword">农作物<span class="markstar">&nbsp;*</span></label>
            <input type="text" id="CropName" value="" placeholder="请输入" class="autocomplete" onblur="CropSelect()" />
            <input type="hidden" id="Cropid" value="" />
            <label class="selectitem" id="otherscroplabel" style="display:none">其它<span class="markstar">&nbsp;*</span></label>
            <input type="text" id="CroplabelName" value="" placeholder="请输入" style="display:none" maxlength="50" />
        </div>
        <div class="newtrial">
            <label class="trialword">季节</label>
            <input type="text" style="width:300px" id="trialKind" maxlength="50" />
        </div>
        <div class="newtrial" style="display:none">
            <label class="trialword">作物品种</label>
            <input type="text" style="width:300px" id="triasublkind" maxlength="50" />
        </div>
        <div class="newtrial">
            <label class="trialword">示范目的<span class="markstar">&nbsp;*</span></label>
            <input type="text" style="width:800px" id="trialaim" maxlength="200" />
        </div>
        <div class="newtrial">
            <label class="trialword">供试药剂<span class="markstar">&nbsp;*</span></label>
            <input type="text" style="width:800px" id="trailmedicine" maxlength="50" />
        </div>
        <div class="newtrial">
            <label class="trialword">防除对象<span class="markstar">&nbsp;*</span></label>
            <input type="text" style="width:800px" id="trialapplied" maxlength="50" />
        </div>

        <div class="newtrial">
            <label class="trialword" style="margin:auto 0;float:left;margin-left:20px">示范方案<span class="markstar">&nbsp;*</span></label>
            <textarea style="width:800px;min-height:100px" id="trialsupplement"></textarea>
        </div>
        <div style="text-align:center;margin:20px;">
            <a href="javascript:;" class="dis-inlineb button w60Btn" onclick="Return()">返回</a>
            <a href="javascript:;" class="dis-inlineb button w60Btn" onclick="NextStep();">保存</a>
        </div>
    </div>


    <div class="trialSecondPage" style="display:none">
        <label class="lableword"><span class="splitline">|</span>基本信息</label>
        <div>
            <label class="selectitem">TTM</label> <span>@UserHelper.GetCurrentAdminUser().Name</span>
            <label class="selectitem">项目类别</label><span id="projectCategoryDisplay"></span>

            <label class="selectitem">CA</label><span id="CANameDisplay"></span>
            <label class="selectitem">FSM</label><span id="FSMNameDisplay"></span>
        </div>
        <div style="background-color:#ffffff">
            <div class="newtrial">
                <label class="trialword">产品类别</label><span id="productCategoryDisplay"></span>
                <label class="trialword">产品</label><span id="productDisplay"></span>
            </div>
            <div class="newtrial">
                <label class="trialword">农作物</label><span id="CropDisplay"></span>
            </div>
            <div class="newtrial">
                <label class="trialword">季节</label><span id="trialKindDisplay"></span>
            </div>
            <div class="newtrial" style="display:none">
                <label class="trialword">作物品种</label><span id="triasublkindDisplay"></span>
            </div>
            <div class="newtrial">
                <label class="trialword">示范目的</label><span id="trialaimDisplay"></span>
            </div>
            <div class="newtrial">
                <label class="trialword">供试药剂</label><span id="trailmedicineDisplay"></span>
            </div>
            <div class="newtrial">
                <label class="trialword">防除对象</label><span id="trialappliedDisplay"></span>
            </div>

            <div class="newtrial">
                <label class="trialword">示范方案</label><span id="trialsupplementDisplay"></span>
            </div>
        </div>
        <div style="text-align:center;margin:20px;">
            <button type="button" class="btn btn-default" id="trialPurposeEdit">&nbsp;编辑&nbsp;</button>
        </div>

        <label class="lableword"><span class="splitline">|</span>选择地点</label>
        <div>
            <input type="hidden" id="ProvinceId" value="0" />
        </div>
        <div class="provinceSelect">
            <ul></ul>
        </div>
        <div style="text-align:center;margin:20px;">
            <button type="button" class="btn btn-default" id="searchPromoterByProvince" onclick="SearchPromoterByProvince()">&nbsp;查询&nbsp;</button>
        </div>
        <div class="searchCont" style="width:500px">
            <!-- jqGrid table list4 -->
            <table id="list4"></table>
            <!-- jqGrid 分页 div gridPager -->
            <div id="gridPager"></div>
        </div>
        <div style="text-align:center;margin:20px;margin-bottom:200px">
            <button type="button" class="btn btn-default" id="assaginTrial">&nbsp;分配&nbsp;</button>
        </div>
    </div>
</div>
<script>
    $().ready(function () {
        GetProvince();
        AutoCompleteCrop();
        GetProjectCategories();
        $(".provinceSelect ul").delegate("a", "click", function () {
            if ($(this).hasClass('active')) {
                $(this).removeClass('active')
            } else {
                $(this).addClass("active");
            }
        })
    })
    var isProcess = 0;
    $("#assaginTrial").on("click", function () {
        if (isProcess == 0) {
            isProcess == 1;

            var records = $('#list4').jqGrid('getGridParam', 'selarrrow');
            if (ValidateInputData() == true) {
                if (records.length <= 0) {
                    AlertErrorBox("请至少选择一条记录");
                } else {
                    //var reg = new RegExp("\r\n", "g");
                    Loading();
                    var data = { vTrialGuide: {}, trialCandidateList: [], projectCategoryID: {}, ca: {}, fsm: {} };
                    data.vTrialGuide.Purpose = $('#trialaim').val();
                    data.vTrialGuide.Agentia = $('#trailmedicine').val();
                    data.vTrialGuide.Object = $('#trialapplied').val();
                    data.vTrialGuide.CropCategory = $('#trialKind').val();
                    data.vTrialGuide.SubCropCategory = $('#triasublkind').val();
                    data.vTrialGuide.Others = encodeTextAreaString($('#trialsupplement').val());
                    data.vTrialGuide.ProductID = $("#productItem option:selected").val();
                    data.vTrialGuide.CropID = $("#Cropid").val();
                    if ($("#productItem option:selected").text() == "其它") {
                        data.vTrialGuide.ProductName = $("#ProductName").val();
                    } else {
                        data.vTrialGuide.ProductName = $("#productItem option:selected").text();
                    }
                    if ($("#CropName").val() == "其它") {
                        data.vTrialGuide.CropName = $("#CroplabelName").val();
                    } else {
                        data.vTrialGuide.CropName = $("#CropName").val();
                    }
                    var rowData;
                    $.each(records, function (index, rowId) {
                        rowData = $("#list4").jqGrid('getRowData', rowId);
                        data.trialCandidateList.push({ PromoterID: rowData.PromoterID, ProvinceID: rowData.CityID, PromoterUserName: rowData.PromoterUserName });
                    });
                    data.projectCategoryID = $("#projectCategory option:selected").val();
                    data.ca = $('#CAName').val();
                    data.fsm = $('#FSMName').val();
                    ProcessAjaxRequest(fixUrl("api/Trial/AssignTask"), "post", data, function (data) {
                        if (data.status == "Success") {
                            AlertCompleteBox("分配成功");
                            setTimeout(function () {
                                location.href = fixUrl("Trial/index")
                            }, 2000)
                        } else {
                            layer.closeAll();
                            AlertCompleteBox("分配失败");
                            isProcess = 0;
                        }

                    })
                }
            }
        } else {
            AlertErrorBox("请勿重复提交");
        }
    })

    function NextStep() {
        if (ValidateInputData() == true) {
            $('.trialFirstPage').css('display', 'none');
            $('.trialSecondPage').css('display', 'block');
            $("#list4").setGridWidth($('.searchCont').width() * 0.99);
            $('#projectCategoryDisplay').text($("#projectCategory option:selected").text());
            $('#CANameDisplay').text($('#CAName').val());
            $('#FSMNameDisplay').text($('#FSMName').val());
            $('#productCategoryDisplay').text($("#productCategory option:selected").text());
            if ($("#productItem option:selected").text() == "其它") {
                $('#productDisplay').text($("#ProductName").val());
            } else {
                $('#productDisplay').text($("#productItem option:selected").text());
            }
            if ($('#CropName').val() == "其它") {
                $('#CropDisplay').text($('#CroplabelName').val());
            } else {
                $('#CropDisplay').text($('#CropName').val());
            }
            $('#trialaimDisplay').text($('#trialaim').val());
            $('#trailmedicineDisplay').text($('#trailmedicine').val());
            $('#trialappliedDisplay').text($('#trialapplied').val());
            $('#trialKindDisplay').text($('#trialKind').val());
            $('#triasublkindDisplay').text($('#triasublkind').val());
            $('#trialsupplementDisplay').text($('#trialsupplement').val());
            var reg = new RegExp("\r\n", "g");
            localStorage.setItem("projectID", $("#projectCategory option:selected").val());
            localStorage.setItem("productCategoryID", $("#productCategory option:selected").val());
            localStorage.setItem("productID", $("#productItem option:selected").val());
            localStorage.setItem("OthersID", $("#ProductName").val());
            localStorage.setItem("Supplement", document.getElementById("trialsupplement").value.replace(reg, "\r\n"));
            localStorage.setItem("cropName", $("#CropName").val());
            localStorage.setItem("OthersCropName", $("#CroplabelName").val());

        }
    }

    $("#trialPurposeEdit").on('click', (function () {
        $('.trialFirstPage').css('display', 'block');
        $('.trialSecondPage').css('display', 'none');
    }))


    $().ready(function () {
        $("#list4").jqGrid({
            postData: { provinceid: $("#ProvinceId").val(), product: $("#Productid").val(), crop: $("#Cropid").val(), statuscode: $(".state li.active").val() },
            Regional: "cn",
            datatype: "json", //数据来源，本地数据
            mtype: "GET",//提交方式
            height: "auto",//高度，表格高度。可为数值、百分比或'auto'
            width: 200,//这个宽度不能为百分比
            autowidth: true,//自动宽
            colNames: ['实验员', '省份', '', '', ''],
            colModel: [
                { name: 'Promoter', index: 'Promoter', width: '25%', align: 'center' },
                { name: 'City', index: 'City', width: '20%', align: "center" },
                { name: 'PromoterID', index: 'PromoterID', width: '25%', align: 'center', hidden: true },
                { name: 'CityID', index: 'CityID', width: '20%', align: "center", hidden: true },
                { name: 'PromoterUserName', index: 'PromoterUserName', width: '20%', align: "center", hidden: true },

            ],
            multiselect: true,
            viewrecords: true,//是否在浏览导航栏显示记录总数
            rowNum: 999,//每页显示记录数
            rowList: [20, 50, 100],//用于改变显示行数的下拉列表框的元素数组。
            jsonReader: {
                id: "ID",
            },
            onSortCol: function (index, colindex, sortorder) {
                //列排序事件，向server传值，值为当前的页数
                jQuery("#list4").jqGrid('setGridParam', { page: $('#gridPager').val() });
            },
            gridComplete: function (data) {
                if ($("#sp_1_gridPager").text() == 1) {
                    $(".ui-pg-input").attr('disabled', 'disabled')
                };
                $("#list4").setGridWidth($('.searchCont').width() * 0.99);

            },
            loadError: function (data) {
                if (data.status == '401') {
                    AlertErrorBox("当前用户未登录，或登录已超时");
                }
            },
            loadonce: true,
            pagination: true,
            //pager: $('#gridPager')
        });
        // 隐藏水平滚动条
        $("#list4").closest(".ui-jqgrid-bdiv").css({ "overflow-x": "hidden" });

        // 显示垂直滚动条
        $("#list4").closest(".ui-jqgrid-bdiv").css({ "overflow-y": "scroll", "max-height": "450px" });

        //JQ grid 自适应宽度
        $(window).resize(function () {
            $("#list4").setGridWidth($('.searchCont').width() * 0.99);
        });

    })

    function SearchPromoterByProvince() {
        if (GetSelectProvinces() == "") {
            AlertErrorBox("至少需要选择一个省份");
        } else {
            $("#list4").jqGrid('setGridParam', {
                postData: { provinceid: GetSelectProvinces(), },
                url: fixUrl("api/Trial/GetPromoterTrialList"),
                datatype: 'json',
                page: 1
            }).trigger("reloadGrid"); //重新载入
        }
    }

    var errormsg = "";
    function ValidateInputData() {
        var errormsg = "";
        if ($("#projectCategory").val <= 0) {
            errormsg += "项目类别不能为空" + "<br>";
        }
        if ($("#productCategory").val() <= 0) {
            errormsg += "产品类别不能为空" + "<br>";
        }
        if ($("#productItem option:selected").text() == "其它") {
            if ($("#ProductName").val() == "") {
                errormsg += "请填写产品名称" + "<br>";
            }
        } if ($("#productItem").val() <= 0) {
            errormsg += "产品不能为空" + "<br>";
        } if ($("#CropName").val() == "") {
            errormsg += "农作物不能为空" + "<br>";
        } else if ($("#CropName").val() == "其它") {
            if ($('#CroplabelName').val() == "") {
                errormsg += "农作物其它不能为空" + "<br>";
            }
        }

        if ($("#trialaim").val() == "") {
            errormsg += "示范目的不能为空" + "<br>";
        } if ($("#trailmedicine").val() == "") {
            errormsg += "供试药剂不能为空" + "<br>";
        } if ($("#trialapplied").val() == "") {
            errormsg += "防除对象不能为空" + "<br>";
        }
        //if ($("#trialKind").val() == "") {
        //    errormsg += "品种不能为空" + "<br>";
        //} if ($("#triasublkind").val() == "") {
        //    errormsg += "子品种不能为空" + "<br>";
        //}
        if ($("#trialsupplement").val() == "") {
            errormsg += "示范方案不能为空" + "<br>";
        }
        if (errormsg.length > 0) {
            AlertMsgBoxWithFunction('提示', errormsg);
            return false;
        }
        return true;
    }

    function ProductSelect() {
        if ($("#productItem option:selected").text() == "其它") {
            $("#othersproductlabel").css("display", "inline");
            $("#ProductName").css("display", "inline");
        } else {
            $("#othersproductlabel").css("display", "none");
            $("#ProductName").css("display", "none");
            $("#ProductName").val("");
        }
    }

    function CropSelect() {
        if ($("#CropName").val() == "其它") {
            $("#otherscroplabel").css("display", "inline");
            $("#CroplabelName").css("display", "inline");
        } else {
            $("#otherscroplabel").css("display", "none");
            $("#CroplabelName").css("display", "none");
            $("#CroplabelName").val("");
        }
    }

    function Return() {
        if ($('#CANameDisplay').text() == "") {
            AlertErrorBox("未填写任何内容");
        } else {
            $('.trialFirstPage').css('display', 'none');
            $('.trialSecondPage').css('display', 'block');
            $("#projectCategory").val(localStorage.getItem("projectID"));
            $("#productCategory").val(localStorage.getItem("productCategoryID"));
            if ($("#productItem option:selected").val() == undefined) {
                setTimeout(function () {
                    $("#productItem").val(localStorage.getItem("productID"));
                }, 2000)
            }
            if (localStorage.getItem("cropName") == "其它") {
                $("#CropName").val(localStorage.getItem("cropName"));
                $("#CroplabelName").val(localStorage.getItem("OthersCropName"));
                $("#otherscroplabel").css("display", "inline");
                $("#CroplabelName").css("display", "inline");
            } else {
                $("#CropName").val(localStorage.getItem("cropName"));
                $("#CroplabelName").val("");
                $("#otherscroplabel").css("display", "none");
                $("#CroplabelName").css("display", "none");
            }
            if (localStorage.getItem("othersID") != null) {
                $("#ProductName").val(localStorage.getItem("othersID"));
                $("#othersproductlabel").css("display", "inline");
                $("#ProductName").css("display", "inline");
            } else {
                $("#ProductName").val("");
                $("#othersproductlabel").css("display", "none");
                $("#ProductName").css("display", "none");
            }

            $('#CAName').val($('#CANameDisplay').text());
            $('#FSMName').val($('#FSMNameDisplay').text());
            $('#CropName').val($('#CropDisplay').text());
            $('#trialaim').val($('#trialaimDisplay').text());
            $('#trailmedicine').val($('#trailmedicineDisplay').text());
            $('#trialapplied').val($('#trialappliedDisplay').text());
            $('#trialKind').val($('#trialKindDisplay').text());
            $('#triasublkind').val($('#triasublkindDisplay').text());
            $('#trialsupplement').val((localStorage.getItem("Supplement")));
        }
    }
</script>
